using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using usm.SolDev.DataBase.DataObjects;
using System.Diagnostics;
using usm.SolDev.DataBase.DataObjects.Experiment;
using ZedGraph;

namespace usm.SolDev.UI.Charting
{
    /// <summary>
    ///     Charting for projects
    /// </summary>
    public partial class ChartForm : DockContent
    {
        #region Private fields
        /// <summary>
        ///     The chart data and settings
        /// </summary>
        private ChartDataObject chartDataObject;
        #endregion

        #region Constructors 
        /// <summary>
        ///     Default constructor
        /// </summary>
        public ChartForm()
        {
            InitializeComponent();
        }

        /// <summary>
        ///     Initialization constructor
        /// </summary>
        /// <param name="chartDataObject">the chart related data</param>
        public ChartForm(ChartDataObject chartDataObject) : this ()
        {
            if (DesignMode == true || chartDataObject == null)
            {
                return;
            }

            this.chartDataObject = chartDataObject;
            TabText = String.Format("results for project : {0}", chartDataObject.Project.Description);
            chartDataObject.ParsedResults = new Parser(chartDataObject.Project);
            
        }
        #endregion

        #region Private properties
        ///  Indicates if the current view is being utilized in the VS.NET
        ///  IDE or not. The similarly-named .Net framework property for 
        ///  a UserControl will only show that its related object in 
        ///  is DesignMode only if the immediate parent is viewed in the IDE. 
        protected static new bool DesignMode
        {
            get
            {
                return (
                        Process.GetCurrentProcess().ProcessName.Equals("VCSExpress") ||
                        Process.GetCurrentProcess().ProcessName.Equals("devenv")
                    );
            }
        }
        #endregion

        #region Public properties
        /// <summary>
        ///     Contains the describing chart data
        /// </summary>
        public ChartDataObject ChartData
        {
            set 
            {
                chartDataObject = value;
                TabText = String.Format("results for project : {0}", chartDataObject.Project.Description);
                zgChart.GraphPane.CurveList.Clear();

                // Illustrate selected process activity during experiment 
                if ( chartDataObject.ChartType == ChartType.ctDiagram && 
                     chartDataObject.ActiveEntity != null && 
                     chartDataObject.ActiveEntity.Type == Entity.EntityType.etProcess )
                {

                    zgChart.GraphPane.Title.Text = String.Format("The activity of process {0} from experiment {1}", chartDataObject.ActiveEntity.Name, chartDataObject.Experiment);
                    zgChart.GraphPane.XAxis.Title.Text = "Simulation time (units)";
                    zgChart.GraphPane.YAxis.Title.Text = "Number of active processes";

                    
                    PointPairList pointsList = new PointPairList();
                    int procCount = 0;
                    foreach(Transaction transact in chartDataObject.ParsedResults.Experiments[chartDataObject.Experiment].Transactions )
                    {
                        if (transact.ProcessName.Equals(chartDataObject.ActiveEntity.Name))
                        {
                            if (transact.Operation == OperationType.otNewTransaction)
                            {
                                procCount += 1; // transact.Number;
                            }

                            if (transact.Operation == OperationType.otCancel)
                            {
                                procCount -= 1; // transact.Number;
                            }

                            pointsList.Add(transact.SimulationTime, procCount);
                        }
                    }

                    zgChart.GraphPane.AddCurve(chartDataObject.ActiveEntity.Name, pointsList, Color.Red);
                    zgChart.RestoreScale(zgChart.GraphPane);
                }

            }
            get
            {
                return chartDataObject;
            }
        }

        #endregion
    }
}